package 线程概念等作业;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-12-21
 * Time: 20:23
 */
public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        int[] array=new int[1000];
        Random random=new Random();
        for (int i = 0; i < array.length; i++) {
            array[i]=random.nextInt(100)+1;
        }
        Thread1 thread1=new Thread1(array);
        Thread2 thread2=new Thread2(array);

        Thread thread11=new Thread(thread1);
        Thread thread22=new Thread(thread2);

        thread11.start();
        thread22.start();
        thread11.join();
        thread22.join();

        int s=thread1.getS()+thread2.getS();
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        System.out.println("程序运行时间：" + duration + " 毫秒");
        System.out.println("数组 总合:"+ s);
    }
}


class Thread1 implements Runnable{
    int[] array;
    int s=0;
    Thread1(int[] array){
        this.array=array;
    }
    @Override
    public void run() {
        for (int i = 0; i < array.length; i+=2) {
            s+=array[i];
        }
        System.out.println("偶数和:"+ s);
    }

    int getS(){
        return s;
    }
}

class Thread2 implements Runnable{
    int[] array;
    int s=0;
    Thread2(int[] array){
        this.array=array;
    }
    @Override
    public void run() {
        for (int i = 1; i < array.length; i+=2) {
            s+=array[i];
        }
        System.out.println("奇数和:"+ s);
    }

    int getS(){
        return s;
    }
}